import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

/* A class to calculate customized grades */
public class GradeCalculator
{
	private String dropList;
	private ArrayList arrDrops;
	private ArrayList arrCategories;
	private ArrayList arrWeights;
	private JTable tblGrades;
	private JTable tblNames;
	private CellData[][] cldGrades;
	private AssignmentData[] asdInfo;
	private StudentData[] stuInfo;
	private String strReport;
	private TableColumnModel columnModel;
	private int intHWTotal;
	private int intQTotal;
	private int intETotal;
	private int intHWMax;
	private int intQMax;
	private int intEMax;
	
	public GradeCalculator(JTable pGrades, JTable pNames, CellData[][] pCldGrades, AssignmentData[] pAsdInfo, StudentData[] pStuInfo)
	{
		dropList = "";
		arrDrops = new ArrayList();
		arrCategories = new ArrayList();
		arrWeights = new ArrayList();
		tblGrades = pGrades;
		tblNames = pNames;
		cldGrades = pCldGrades;
		asdInfo = pAsdInfo;
		stuInfo = pStuInfo;
		strReport = "";
		columnModel = tblGrades.getColumnModel();
		intHWTotal = 0;
		intQTotal = 0;
		intETotal = 0;
		intHWMax = 0;
		intQMax = 0;
		intEMax = 0;
	}
	
	public void drop(String pAssignment)
	{
		arrDrops.add(pAssignment);
	}
	
	public void weight(String pCategory, String pPercent)
	{
		arrCategories.add(pCategory);
		arrWeights.add(pPercent);
	}
	
	public String calculate()
	{
		for(int i = 0; i < tblGrades.getRowCount(); i++)
		{
			if(tblNames.getValueAt(i, 0) != null)
			{
				for(int j = 0; j < tblGrades.getColumnCount(); j++)
				{
					if(arrDrops.indexOf(columnModel.getColumn(j).getHeaderValue()) == -1 && !tblGrades.getValueAt(i, j).equals(""))
					{
						if(i == 0)
						{
							int temp = Integer.parseInt(asdInfo[j].getPoints());
							if(asdInfo[j].getCategory().equals("homework"))
								intHWMax = intHWMax + temp;
							if(asdInfo[j].getCategory().equals("quiz"))
								intQMax = intQMax + temp;
							if(asdInfo[j].getCategory().equals("exam"))
								intEMax = intEMax + temp;
						}
						int tempValue = Integer.parseInt(tblGrades.getValueAt(i,j).toString());
						if(asdInfo[j].getCategory().equals("homework"))
							intHWTotal = intHWTotal + tempValue;
						if(asdInfo[j].getCategory().equals("quiz"))
							intQTotal = intQTotal + tempValue;
						if(asdInfo[j].getCategory().equals("exam"))
							intETotal = intETotal + tempValue;
					}
				}
				strReport = strReport + tblNames.getValueAt(i, 0);
				int tempWeightOne = Integer.parseInt(arrWeights.get(0).toString());
				int tempWeightTwo = Integer.parseInt(arrWeights.get(1).toString());
				int tempWeightThree = Integer.parseInt(arrWeights.get(2).toString());
				double tempHW = 0;
				double tempQ = 0;
				double tempE = 0;
				if(intHWMax != 0)
					tempHW = ((double)intHWTotal)/((double)intHWMax) * (tempWeightOne * .01);
				if(intQMax != 0)
					tempQ = ((double)intQTotal)/((double)intQMax) * (tempWeightTwo * .01);
				if(intEMax != 0)
					tempE = ((double)intETotal)/((double)intEMax) * (tempWeightThree * .01);
				
				double finalGrade = tempHW + tempQ + tempE;
				//System.out.println(tempWeightOne + " " + tempWeightTwo + " " + tempWeightThree + " " + tempHW + " " + tempQ + " " + tempE);
				strReport = strReport + ": " + finalGrade + "%\n";
				intHWTotal = 0;
				intQTotal = 0;
				intETotal = 0;
			}
		}

		return strReport;
	}
}